package homework;

import java.util.*;

/*
    1.分析以下需求，并用代码实现：
	(1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
	(2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
	(3)利用四种方式遍历Map集合中的内容，格式：key::value
	*/
public class Test1 {
        public static void main(String[] args) {
            Map<Student, String> map = new HashMap<>();
            Student s1 = new Student("阿童木", 18);
            Student s2 = new Student("百变小樱", 13);
            Student s3 = new Student("哆啦A梦", 100);
            map.put(s1, "长沙岳麓区");
            map.put(s2, "北京东3环");
            map.put(s3, "河北秦皇岛");

            //    迭代器 通过键
            Set<Student> keys = map.keySet();
            Iterator<Student> it = keys.iterator();
            while (it.hasNext()){
                Student key = it.next();
                String value = map.get(keys);
                System.out.println(key+"----"+value);
            }
        }

    private static void method4(Map<Student, String> map) {
        //第四种遍历  迭代器 通过键值对
        Iterator<Map.Entry<Student, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Student, String> next = it.next();
            System.out.println(next);
        }
    }

    private static void method3(Map<Student, String> map) {
        //第三种遍历   forEach
        map.forEach(
                (Student key, String value) -> {
                    System.out.println(key + "----" + value);
                }
        );
    }

    private static void method2(Map<Student, String> map) {
        //第二种遍历增强for     通过键值对
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "----" + value);
        }
    }

    private static void method1(Map<Student, String> map) {
        //第一种遍历增强for   通过键
        Set<Student> keys = map.keySet();
        for (Student key : keys) {
            String value = map.get(key);
            System.out.println(key + "----" + value);
        }
    }
}
